//
//  EditUserInfoV2ViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/6.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "EditUserInfoV2ViewModel.h"

#import "DeclarationInfosData.h"

@implementation PersonSubmitData

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        _nickname = [um.userData.nickname isNotBlank] ? um.userData.nickname : ([um.userData.wxNickName isNotBlank] ? um.userData.wxNickName : @"");
        _sex = [um.userData.sex isNotBlank] ? [NSString stringWithFormat:@"%@", um.userData.sex] : @"0";
        _zone = [um.userData.areaID isNotBlank] ? um.userData.areaID : @"";
        _sy = [um.userData.declaration isNotBlank] ? um.userData.declaration : @"";
        _birth = [um.userData.birth isNotBlank] ? um.userData.birth : @"";
    }
    
    return self;
}

- (void)setSex:(NSString *)sex
{
    if ([sex isEqualToString:@"男"])
    {
        _sex = @"1";
    }
    else if ([sex isEqualToString:@"女"])
    {
        _sex = @"2";
    }
    else
    {
        _sex = @"0";
    }
}

@end

@implementation ExpertSubmitData

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        self.contacts = [um.expertData.expertInfo.contacts isNotBlank] ? um.expertData.expertInfo.contacts : @"";
        self.mobile = [um.expertData.expertInfo.mobile isNotBlank] ? um.expertData.expertInfo.mobile : @"";
        self.intro = [um.expertData.expertInfo.summary isNotBlank] ? um.expertData.expertInfo.summary : @"";
    }
    
    return self;
}

@end

@implementation EnterpriseSubmitData

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        DataManager* dm = [DataManager sharedDataManager];
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        self.intro = [um.enterpriseData.enterpriseInfo.summary isNotBlank] ? um.enterpriseData.enterpriseInfo.summary : @"";
        self.name = [um.enterpriseData.enterpriseInfo.name isNotBlank] ? um.enterpriseData.enterpriseInfo.name : @"";
        self.contacterName = [um.enterpriseData.enterpriseInfo.contacts isNotBlank] ? um.enterpriseData.enterpriseInfo.contacts : @"";
        self.mobile = [um.enterpriseData.enterpriseInfo.mobile isNotBlank] ? um.enterpriseData.enterpriseInfo.mobile : @"";
        self.address = [um.enterpriseData.enterpriseInfo.address isNotBlank] ? um.enterpriseData.enterpriseInfo.address : @"";
        self.zoneData = [dm findCityDataBy:um.enterpriseData.enterpriseInfo.areaID];
    }
    
    return self;
}

@end

@implementation InsutitutionSumitData

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        DataManager* dm = [DataManager sharedDataManager];
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        self.intro = [um.institutionData.institutionInfo.summary isNotBlank] ? um.institutionData.institutionInfo.summary : @"";
        self.servContent = [um.institutionData.institutionInfo.service isNotBlank] ? um.institutionData.institutionInfo.service : @"";
        //TODO: 机构分类写死
        self.category = um.institutionData.institutionInfo.category;
        self.name = [um.institutionData.institutionInfo.name isNotBlank] ? um.institutionData.institutionInfo.name : @"";
        self.mobile = [um.institutionData.institutionInfo.mobile isNotBlank] ? um.institutionData.institutionInfo.mobile : @"";
        self.contacts = [um.institutionData.institutionInfo.contacts isNotBlank] ? um.institutionData.institutionInfo.contacts : @"";
        self.address = [um.institutionData.institutionInfo.address isNotBlank] ? um.institutionData.institutionInfo.address : @"";
        self.zoneData = [dm findCityDataBy:um.institutionData.institutionInfo.areaID];
    }
    
    return self;
}

@end



@implementation EditUserInfoV2ViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    DataManager* dm = [DataManager sharedDataManager];
    
    _type = 0;
    
    _showPickerTag = 0;
    
    _ctrlTag = -1;
    
    _personSubmitData = [[PersonSubmitData alloc] init];
    _expertSubmitData = [[ExpertSubmitData alloc] init];
    _enterpriseSubmitData = [[EnterpriseSubmitData alloc] init];
    _insutitutionSumitData = [[InsutitutionSumitData alloc] init];
    
    RAC(self, nickname) = [[RACObserve(self.personSubmitData, nickname) distinctUntilChanged] map:^id(NSString* nn) {
        return [nn isNotBlank] ? nn : @"生态云用户";
    }];
    RAC(self, sy) = [[RACObserve(self.personSubmitData, sy) distinctUntilChanged] map:^id(id value)
     {
         NSString* strValue = value;
         
         return (strValue!=nil && [strValue isNotBlank]) ? @"已完善" : @"未完善";
    }];
    RAC(self, sex) = [[RACObserve(self.personSubmitData, sex) distinctUntilChanged] map:^id(id value)
    {
        NSString* sexValue = value;
        
        return [sexValue isEqualToString:@"0"] ? @"未知" : ([sexValue isEqualToString:@"1"] ? @"男" : @"女");
    }];
    RAC(self, birth) = [RACObserve(self.personSubmitData, birth) map:^id(id value)
    {
        NSString* strBirth = value;
        return (strBirth!=nil && [strBirth isNotBlank]) ? strBirth : @"未完善";
    }];
    RAC(self, zone) = [RACObserve(self.personSubmitData, zone) map:^id(id value)
    {
        NSString* strZone = value;
        
        if ((strZone!=nil && [strZone isNotBlank]))
        {
            if ([strZone isEqualToString:@"0"])
            {
                strZone = @"全国";
            }
            else
            {
                ProvinceListData* findResult = nil;
                
                for (ProvinceListData* pListData in dm.addrData.datas)
                {
                    for (ProvinceListData* spListData in pListData.datas)
                    {
                        if ([spListData.areaID isEqualToString:strZone])
                        {
                            findResult = spListData;
                            
                            break;
                        }
                    }
                    
                    if (findResult)
                        break;
                }
                
                if (findResult)
                {
                    strZone = findResult.name;
                }
            }
        }
        
        return (strZone!=nil && [strZone isNotBlank]) ? strZone : @"未完善";
    }];
    
    _companyIntro = @"";
    _companyContact = @"";
    _mechanismIntro = @"";
    _mechanismContact = @"";
    _mechanismServContent = @"";
    
    _personCertImages = [NSMutableArray array];
    _busyCertImages = [NSMutableArray array];
    _aboutCertImages = [NSMutableArray array];
    
    _personCertImagesChangeSignal = [RACObserve(self, personCertImages) map:^id(NSArray* imgDatas)
                                     {
                                         NSArray *imgurls = [imgDatas.rac_sequence map:^NSString *(NSString* data)
                                                             {
                                                                 return data;
                                                             }].array;
                                         
                                         return [NSMutableArray arrayWithArray:imgurls];
                                     }] ;
    _busyCertImagesChangeSignal = [RACObserve(self, busyCertImages) map:^id(NSArray* imgDatas)
                                     {
                                         NSArray *imgurls = [imgDatas.rac_sequence map:^NSString *(NSString* data)
                                                             {
                                                                 return data;
                                                             }].array;
                                         
                                         return [NSMutableArray arrayWithArray:imgurls];
                                     }] ;
    _aboutCertImagesChangeSignal = [RACObserve(self, aboutCertImages) map:^id(NSArray* imgDatas)
                                     {
                                         NSArray *imgurls = [imgDatas.rac_sequence map:^NSString *(NSString* data)
                                                             {
                                                                 return data;
                                                             }].array;
                                         
                                         return [NSMutableArray arrayWithArray:imgurls];
                                     }] ;
    
    self.addImgCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        @weakify(self);
        return [[self requestUploadImg:input] doNext:^(id x)
        {
            @strongify(self);
            
            EPImageData* imageData = x;
            
            if (self.ctrlTag == 10)
            {
                NSMutableArray* newUploadImgs = [NSMutableArray arrayWithArray:[self.personCertImages arrayByAddingObjectsFromArray:@[imageData]]];
                self.personCertImages = newUploadImgs;
            }
            else if (self.ctrlTag == 11)
            {
                NSMutableArray* newUploadImgs = [NSMutableArray arrayWithArray:[self.busyCertImages arrayByAddingObjectsFromArray:@[imageData]]];
                self.busyCertImages = newUploadImgs;
            }
            else if (self.ctrlTag == 12)
            {
                NSMutableArray* newUploadImgs = [NSMutableArray arrayWithArray:[self.aboutCertImages arrayByAddingObjectsFromArray:@[imageData]]];
                self.aboutCertImages = newUploadImgs;
            }
        }];
    }];
    
    self.deleteImgCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        NSInteger delIndex = [input integerValue];
        
        return [self requestDelUploadImg:delIndex];
    }];
    
    self.changeAvatarCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        return [self requestUploadAvatar:input];
    }];
    
    self.normalUserReqCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        return [self requestSubmitNormalInfoSingal];
    }];
    
    self.enterpriseZoneReqCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        return [self _requestEnterpriseZoneSingal];
    }];
    
    self.institutionZoneReqCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        return [self _requestInstituionZoneSingal];
    }];
    
    self.submitCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        @strongify(self);
        
        return [self requestSubmitSingal];
    }];
}

- (void)setType:(NSInteger)type
{
    _type = type;
    
    DataManager* dm = [DataManager sharedDataManager];
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    switch (_type)
    {
        case 0:
        {
            
        }
            break;
            
        case 1:
        {
            RAC(self, intro) = [RACObserve(self.expertSubmitData, intro) map:^id(id value)
                                {
                                    NSString* strValue = value;
                                    return (strValue!=nil && [strValue isNotBlank]) ? @"已完善" : @"未完善";
                                }];
            
            NSInteger count = um.expertData.expertImage.count;
            for (NSInteger index=0; index<count; index++)
            {
                EPImageData* curImgData = um.expertData.expertImage[index];
                
                if ([curImgData.type isEqualToString:@"1"])
                {
                    _personCertImages[_personCertImages.count] = curImgData.imageurl;
                }
                else
                {
                    _aboutCertImages[_aboutCertImages.count] = curImgData.imageurl;
                }
            }
        }
            break;
            
        case 2:
        {
//            _mczone = [dm findCityDataBy:um.enterpriseData.enterpriseInfo.areaID].name;
            RAC(self, mczone) = [RACObserve(self.enterpriseSubmitData, zoneData) map:^id(id value)
                                 {
                                     ProvinceListData* pListData = value;
                                     NSString* strValue = pListData.name;
                                     return (strValue!=nil && [strValue isNotBlank]) ? strValue : @"未完善";
                                 }];
            
            RAC(self, companyIntro) = [RACObserve(self.enterpriseSubmitData, intro) map:^id(id value)
                                       {
                                           NSString* strValue = value;
                                           return (strValue!=nil && [strValue isNotBlank]) ? @"已完善" : @"未完善";
                                       }];
            
            RAC(self, companyContact) = [RACObserve(self.enterpriseSubmitData, name) map:^id(id value)
                                         {
                                             BOOL result = YES;
                                             
                                             if (self.enterpriseSubmitData.name==nil || ![self.enterpriseSubmitData.name isNotBlank])
                                             {
                                                 result = NO;
                                             }
                                             
                                             if (self.enterpriseSubmitData.contacterName==nil || ![self.enterpriseSubmitData.contacterName isNotBlank])
                                             {
                                                 result = NO;
                                             }
                                             
                                             if (self.enterpriseSubmitData.mobile==nil || ![self.enterpriseSubmitData.mobile isNotBlank])
                                             {
                                                 result = NO;
                                             }
                                             
                                             if (self.enterpriseSubmitData.address==nil || ![self.enterpriseSubmitData.address isNotBlank])
                                             {
                                                 result = NO;
                                             }
                                             
                                             return result ? @"已完善" : @"未完善";
                                         }];
            
            NSInteger count = um.enterpriseData.enterpriseImage.count;
            for (NSInteger index=0; index<count; index++)
            {
                EPImageData* curImgData = um.enterpriseData.enterpriseImage[index];
                
                _busyCertImages[_busyCertImages.count] = curImgData.imageurl;
            }
        }
            break;
            
        case 3:
        {
            RAC(self, mczone) = [RACObserve(self.insutitutionSumitData, zoneData) map:^id(id value)
                                 {
                                     ProvinceListData* pListData = value;
                                     NSString* strValue = pListData.name;
                                     return (strValue!=nil && [strValue isNotBlank]) ? strValue : @"未完善";
                                 }];
            
            RAC(self, mechanismIntro) = [RACObserve(self.insutitutionSumitData, intro) map:^id(id value)
                                   {
                                       NSString* strValue = value;
                                       return (strValue!=nil && [strValue isNotBlank]) ? @"已完善" : @"未完善";
                                   }];;
            
            RAC(self, mechanismContact) = [RACObserve(self.insutitutionSumitData, name) map:^id(id value)
                                           {
                                               BOOL result = YES;
                                               
                                               if (self.insutitutionSumitData.name==nil || ![self.insutitutionSumitData.name isNotBlank])
                                               {
                                                   result = NO;
                                               }
                                               
                                               if (self.insutitutionSumitData.contacts==nil || ![self.insutitutionSumitData.contacts isNotBlank])
                                               {
                                                   result = NO;
                                               }
                                               
                                               if (self.insutitutionSumitData.mobile==nil || ![self.insutitutionSumitData.mobile isNotBlank])
                                               {
                                                   result = NO;
                                               }
                                               
                                               if (self.insutitutionSumitData.address==nil || ![self.insutitutionSumitData.address isNotBlank])
                                               {
                                                   result = NO;
                                               }
                                               
                                               return result ? @"已完善" : @"未完善";
                                           }];
            
            RAC(self, mechanismServContent) = [RACObserve(self.insutitutionSumitData, servContent) map:^id(id value)
                                               {
                                                   NSString* strValue = value;
                                                   return (strValue!=nil && [strValue isNotBlank]) ? @"已完善" : @"未完善";
                                               }];
            
            
            NSInteger count = um.institutionData.institutionImage.count;
            for (NSInteger index=0; index<count; index++)
            {
                EPImageData* curImgData = um.institutionData.institutionImage[index];
                
                if ([curImgData.type isEqualToString:@"1"])
                {
                    _busyCertImages[_busyCertImages.count] = curImgData.imageurl;
                }
                else
                {
                    _aboutCertImages[_aboutCertImages.count] = curImgData.imageurl;
                }
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 功能
- (void)chooseSTXY:(NSString *)text
{
    self.personSubmitData.sy = text;
}

#pragma mark -- 检测提交数据
- (RACSignal*)checkPersonalSubmitData
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                
                return nil;
            }];
    
    NSString* result = @"";
    if (![result isNotBlank] && ![self.personSubmitData.nickname isNotBlank])
    {
        result = @"请填写昵称";
    }
    
    if (![result isNotBlank] && ![self.personSubmitData.sex isNotBlank])
    {
        result = @"请选择性别";
    }
    
    if (![result isNotBlank] && ![self.personSubmitData.zone isNotBlank])
    {
        result = @"请选择地区";
    }
    
    if (![result isNotBlank] && ![self.personSubmitData.sy isNotBlank])
    {
        result = @"请填写宣言";
    }
    
    if (![result isNotBlank] && ![self.personSubmitData.birth isNotBlank])
    {
        result = @"请选择生日";
    }
    
    if ([result isNotBlank])
    {
        return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:result}]];
    }
    else
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
        {
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
            
            return nil;
        }];
    }
}

- (RACSignal*)checkExpertSubmitData
{
    NSString* result = @"";
    if (![result isNotBlank] && ![self.expertSubmitData.intro isNotBlank])
    {
        result = @"请填写简介";
    }
    
    if ([result isNotBlank])
    {
        return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:result}]];
    }
    else
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
}

- (RACSignal*)checkEnterpriseSubmitData
{
    NSString* result = @"";
    if (![result isNotBlank] && ![self.enterpriseSubmitData.intro isNotBlank])
    {
        result = @"请填写简介";
    }
    
    if (![result isNotBlank] && ![self.enterpriseSubmitData.mobile isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if (![result isNotBlank] && ![self.enterpriseSubmitData.contacterName isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if (![result isNotBlank] && ![self.enterpriseSubmitData.name isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if (![result isNotBlank] && ![self.enterpriseSubmitData.address isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if ([result isNotBlank])
    {
        return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:result}]];
    }
    else
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
}

- (RACSignal*)checkInsutitutionSubmitData
{
    NSString* result = @"";
    if (![result isNotBlank] && ![self.insutitutionSumitData.intro isNotBlank])
    {
        result = @"请填写简介";
    }
    
    if (![result isNotBlank] && ![self.insutitutionSumitData.mobile isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if (![result isNotBlank] && ![self.insutitutionSumitData.contacts isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if (![result isNotBlank] && ![self.insutitutionSumitData.name isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if (![result isNotBlank] && ![self.insutitutionSumitData.address isNotBlank])
    {
        result = @"请完善联系方式";
    }
    
    if (![result isNotBlank] && ![self.insutitutionSumitData.servContent isNotBlank])
    {
        result = @"请填写服务内容";
    }
    
    if (![result isNotBlank] && ![self.insutitutionSumitData.category isNotBlank])
    {
        result = @"请选择机构类型";
    }
    
    if ([result isNotBlank])
    {
        return [RACSignal error:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:result}]];
    }
    else
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
}

#pragma mark - 请求部分
- (RACSignal*)requestUploadAvatar:(UIImage*)image
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    NSString* strUserID = um.userData.userID;
    
    CGSize uImgSize = image.size;
    UIImage* newUploadImg = [image hyb_clipToSize:(CGSize){100, 100*uImgSize.height/uImgSize.width} isEqualScale:YES];
    NSString* strImage = [UIImagePNGRepresentation(newUploadImg) base64EncodedStringWithOptions:0];
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                [[AFNetWorkUtils racPOSTWthURL:url_interface_user_uploadimage params:interface_param_user_uploadimage(strUserID, strImage)] subscribeNext:^(id x)
                 {
                     um.userData.headimg = x[@"headimg"];
                     
                     [um updateUserInfo];
                     
                     [subscriber sendNext:x[SUCommandSysMessageKey]];
                     [subscriber sendCompleted];
                     
                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"上传图片失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

- (RACSignal*)requestUploadImg:(UIImage*)image
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    NSString* strUserID = um.userData.userID;
    NSString* reqType = 0;
    
    CGSize uImgSize = image.size;
    UIImage* newUploadImg = [image hyb_clipToSize:(CGSize){100, 100*uImgSize.height/uImgSize.width} isEqualScale:YES];
    NSString* strImage = [UIImagePNGRepresentation(newUploadImg) base64EncodedStringWithOptions:0];
    
    // 专家
    if (self.type == 1)
    {
        reqType = self.ctrlTag == 10 ? @"1" : @"2";
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_uploadexamineimage params:interface_param_uploadexamineimage(strUserID, strImage, reqType)] subscribeNext:^(id x)
                    {
                        [subscriber sendNext:[EPImageData modelWithJSON:x]];
                        [subscriber sendCompleted];
                        
                    } error:^(NSError *error)
                    {
                        NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"上传图片失败";
                        [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                        [subscriber sendCompleted];
                    }];
                    
                    return nil;
                }];
    }
    else if (self.type == 2)
    {
        reqType = self.ctrlTag == 11 ? @"1" : @"2";
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_institution_uploadexamineimage params:interface_param_institution_uploadexamineimage(strUserID, strImage, reqType)] subscribeNext:^(id x)
                    {
                        [subscriber sendNext:[EPImageData modelWithJSON:x]];
                        [subscriber sendCompleted];
                        
                    } error:^(NSError *error)
                    {
                        NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"上传图片失败";
                        [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                        [subscriber sendCompleted];
                    }];
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
    else if (self.type == 3)
    {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_enterprise_uploadexamineimage params:interface_param_enterprise_uploadexamineimage(strUserID, strImage)] subscribeNext:^(id x)
                     {
                         [subscriber sendNext:[EPImageData modelWithJSON:x]];
                         [subscriber sendCompleted];
                         
                     } error:^(NSError *error)
                     {
                         NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"上传图片失败";
                         [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                         [subscriber sendCompleted];
                     }];
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                    
                    return nil;
                }];
    }
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        
        return nil;
    }];
}

- (RACSignal*)requestDelUploadImg:(NSInteger)delIndex
{
    @weakify(self);
    
    // 专家
    if (self.type == 1)
    {
        EPImageData* delImageData = self.ctrlTag == 10 ? self.personCertImages[delIndex] : self.aboutCertImages[delIndex];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    @strongify(self);
                    
                    @weakify(self);
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_deleteexamineimage params:interface_param_deleteexamineimage(delImageData.imageID)] subscribeNext:^(id x)
                     {
                         @strongify(self);
                         
                         if (self.ctrlTag == 10)
                         {
                             [self.personCertImages removeObjectAtIndex:delIndex];
                             self.personCertImages = [NSMutableArray arrayWithArray:self.personCertImages];
                         }
                         else
                         {
                             [self.aboutCertImages removeObjectAtIndex:delIndex];
                             self.aboutCertImages = [NSMutableArray arrayWithArray:self.aboutCertImages];
                         }
                         
                         [subscriber sendNext:x[SUCommandSysMessageKey]];
                         [subscriber sendCompleted];
                         
                     } error:^(NSError *error)
                     {
                         NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"删除图片失败";
                         [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                         [subscriber sendCompleted];
                     }];
                    
                    return nil;
                }];
    }
    //企业
    else if (self.type == 2)
    {
        EPImageData* delImageData = self.ctrlTag == 11 ? self.busyCertImages[delIndex] : self.aboutCertImages[delIndex];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    @strongify(self);
                    
                    @weakify(self);
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_institution_deleteexamineimage params:interface_param_institution_deleteexamineimage(delImageData.imageID)] subscribeNext:^(id x)
                     {
                         @strongify(self);
                         
                         if (self.ctrlTag == 11)
                         {
                             [self.busyCertImages removeObjectAtIndex:delIndex];
                             self.busyCertImages = [NSMutableArray arrayWithArray:self.busyCertImages];
                         }
                         else
                         {
                             [self.aboutCertImages removeObjectAtIndex:delIndex];
                             self.aboutCertImages = [NSMutableArray arrayWithArray:self.aboutCertImages];
                         }
                         
                         [subscriber sendNext:x[SUCommandSysMessageKey]];
                         [subscriber sendCompleted];
                         
                     } error:^(NSError *error)
                     {
                         NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"上传图片失败";
                         [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                         [subscriber sendCompleted];
                     }];
                    
                    return nil;
                }];
    }
    //机构
    else if (self.type == 3)
    {
        EPImageData* delImageData = self.busyCertImages[delIndex];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
                {
                    @strongify(self);
                    
                    @weakify(self);
                    [[AFNetWorkUtils racPOSTWthURL:url_interface_enterprise_deleteexamineimage params:interface_param_enterprise_deleteexamineimage(delImageData.imageID)] subscribeNext:^(id x)
                     {
                         @strongify(self);
                         
                         [self.busyCertImages removeObjectAtIndex:delIndex];
                         self.busyCertImages = [NSMutableArray arrayWithArray:self.busyCertImages];
                         
                         [subscriber sendNext:x[SUCommandSysMessageKey]];
                         [subscriber sendCompleted];
                         
                     } error:^(NSError *error)
                     {
                         NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"上传图片失败";
                         [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                         [subscriber sendCompleted];
                     }];
                    
                    return nil;
                }];
    }
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                
                return nil;
            }];
}

#pragma mark -- 修改用户信息
- (RACSignal*)requestSubmitNormalInfoSingal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* userID = um.userData.userID;
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        @strongify(self);
        
        @weakify(self);
        [[AFNetWorkUtils racPOSTWthURL:url_interface_user_perfectuploads params:interface_param_user_perfectuploads(userID, self.personSubmitData.nickname, self.personSubmitData.sex, self.personSubmitData.zone, self.personSubmitData.sy, self.personSubmitData.birth)] subscribeNext:^(id x)
         {
             [subscriber sendNext:x[SUCommandSysMessageKey]];
             [subscriber sendCompleted];
             
         } error:^(NSError *error)
         {
             NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"提交失败";
             [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
             [subscriber sendCompleted];
         }];
        
        return nil;
    }];
}

#pragma mark -- 修改专家信息
//修改专家简介
- (RACSignal*)requestExpertSummaryEditSingal
{
    @weakify(self);

    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* expertID = um.expertData.expertInfo.expertID;

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);

                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_summaryedit params:interface_param_summaryedit(expertID, self.expertSubmitData.contacts, self.expertSubmitData.mobile, self.expertSubmitData.intro, @"0")] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:x[SUCommandSysMessageKey]];
                     [subscriber sendCompleted];

                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"修改专家信息失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];

                return nil;
            }];
}

//提交专家认证
//- (RACSignal*)requestExpertSubmitExamineSingal
//{
//    @weakify(self);
//
//    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
//    NSString* userID = um.userData.userID;
//
//    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
//            {
//                @strongify(self);
//
//                @weakify(self);
//                [[AFNetWorkUtils racPOSTWthURL:url_interface_submitexamine params:interface_param_submitexamine(userID)] subscribeNext:^(id x)
//                 {
//                     [subscriber sendNext:x[SUCommandSysMessageKey]];
//                     [subscriber sendCompleted];
//
//                 } error:^(NSError *error)
//                 {
//                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"提交专家审核失败";
//                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
//                     [subscriber sendCompleted];
//                 }];
//
//                return nil;
//            }];
//}

#pragma mark -- 修改企业信息
//修改企业地区
- (RACSignal*)_requestEnterpriseZoneSingal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* eID = um.enterpriseData.enterpriseInfo.enterpriseID;
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_enterprise_summaryedit params:interface_param_enterprise_summaryedit(eID, @"", @"", @"", self.enterpriseSubmitData.zoneData.areaID, @"", @"", @"0")] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:x[SUCommandSysMessageKey]];
                     [subscriber sendCompleted];
                     
                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"修改企业地区失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

//修改企业信息
- (RACSignal*)requestEnterpriseSummaryEditSingal
{
    @weakify(self);

    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* eID = um.enterpriseData.enterpriseInfo.enterpriseID;
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);

                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_enterprise_summaryedit params:interface_param_enterprise_summaryedit(eID, self.enterpriseSubmitData.mobile, self.enterpriseSubmitData.contacterName, self.enterpriseSubmitData.name, self.enterpriseSubmitData.zoneData.areaID, self.enterpriseSubmitData.address, self.enterpriseSubmitData.intro, @"0")] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:x[SUCommandSysMessageKey]];
                     [subscriber sendCompleted];

                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"修改企业简介失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];

                return nil;
            }];
}

//提交企业认证
//- (RACSignal*)requestEnterpriseSubmitExamineSingal
//{
//    @weakify(self);
//
//    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
//    NSString* userID = um.userData.userID;
//
//    NSArray* contactcs = [self.companyContact componentsSeparatedByString:@"|"];
//
//    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
//            {
//                @strongify(self);
//
//                @weakify(self);
//                [[AFNetWorkUtils racPOSTWthURL:url_interface_enterprise_submitexamine params:interface_param_enterprise_submitexamine(userID, self.enterpriseSubmitData.mobile, self.enterpriseSubmitData.contacterName, self.enterpriseSubmitData.name, self.enterpriseSubmitData.address)] subscribeNext:^(id x)
//                 {
//                     [subscriber sendNext:x[SUCommandSysMessageKey]];
//                     [subscriber sendCompleted];
//
//                 } error:^(NSError *error)
//                 {
//                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"提交企业认证失败";
//                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
//                     [subscriber sendCompleted];
//                 }];
//
//                return nil;
//            }];
//}

#pragma mark -- 修改机构信息
//修改机构服务内容
//- (RACSignal*)requestInstitutionServiceEditSingal
//{
//    @weakify(self);
//
//    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
//    NSString* iID = um.institutionData.institutionInfo.enterpriseID;
//
//    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
//            {
//                @strongify(self);
//
//                @weakify(self);
//                [[AFNetWorkUtils racPOSTWthURL:url_interface_institution_serviceedit params:interface_param_institution_serviceedit(iID, self.insutitutionSumitData.servContent)] subscribeNext:^(id x)
//                 {
//                     [subscriber sendNext:x[SUCommandSysMessageKey]];
//                     [subscriber sendCompleted];
//
//                 } error:^(NSError *error)
//                 {
//                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"修改机构服务内容失败";
//                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
//                     [subscriber sendCompleted];
//                 }];
//
//                return nil;
//            }];
//}

//修改机构地区
- (RACSignal*)_requestInstituionZoneSingal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* iID = um.institutionData.institutionInfo.institutionID;
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);
                
                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_institution_summaryedit params:interface_param_institution_summaryedit(iID, @"", @"", @"", @"", @"", self.insutitutionSumitData.zoneData.areaID, @"", @"", @"0")] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:x[SUCommandSysMessageKey]];
                     [subscriber sendCompleted];
                     
                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"修改机构地区失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];
                
                return nil;
            }];
}

//修改机构简介
- (RACSignal*)requestInstitutionSummaryEditSingal
{
    @weakify(self);

    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* iID = um.institutionData.institutionInfo.institutionID;

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
            {
                @strongify(self);

                @weakify(self);
                [[AFNetWorkUtils racPOSTWthURL:url_interface_institution_summaryedit params:interface_param_institution_summaryedit(iID, self.insutitutionSumitData.category, self.insutitutionSumitData.mobile, self.insutitutionSumitData.contacts, self.insutitutionSumitData.name, self.insutitutionSumitData.address, self.insutitutionSumitData.zoneData.areaID, self.insutitutionSumitData.servContent, self.insutitutionSumitData.intro, @"0")] subscribeNext:^(id x)
                 {
                     [subscriber sendNext:x[SUCommandSysMessageKey]];
                     [subscriber sendCompleted];

                 } error:^(NSError *error)
                 {
                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"修改机构简介失败";
                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
                     [subscriber sendCompleted];
                 }];

                return nil;
            }];
}

//提交机构认证
//- (RACSignal*)requestInstitutionSumitExamineSingal
//{
//    @weakify(self);
//
//    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
//    NSString* userID = um.userData.userID;
//
//    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
//            {
//                @strongify(self);
//
//                @weakify(self);
//                [[AFNetWorkUtils racPOSTWthURL:url_interface_institution_submitexamine params:interface_param_institution_submitexamine(userID, self.insutitutionSumitData.category, self.insutitutionSumitData.mobile, self.insutitutionSumitData.contacts, self.insutitutionSumitData.name, self.insutitutionSumitData.address, self.insutitutionSumitData.zoneData.areaID)] subscribeNext:^(id x)
//                 {
//                     [subscriber sendNext:x[SUCommandSysMessageKey]];
//                     [subscriber sendCompleted];
//
//                 } error:^(NSError *error)
//                 {
//                     NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"提交机构认证失败";
//                     [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
//                     [subscriber sendCompleted];
//                 }];
//
//                return nil;
//            }];
//}

- (RACSignal*)requestSubmitSingal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    NSString* userID = um.userData.userID;
    NSInteger identify = [um.userData.identify integerValue];
    
    if (identify == 1)
    {
        return [[self checkPersonalSubmitData] concat:[self requestSubmitNormalInfoSingal]];
    }
    else if (identify == 2)
    {
        return [[[[self checkPersonalSubmitData] concat:[self checkExpertSubmitData]] concat:[self requestSubmitNormalInfoSingal]] concat:[self requestExpertSummaryEditSingal]];
    }
    else if (identify == 3)
    {
        return [[[[self checkPersonalSubmitData] concat:[self checkEnterpriseSubmitData]] concat:[self requestSubmitNormalInfoSingal]] concat:[self requestEnterpriseSummaryEditSingal]];
    }
    else if (identify == 4)
    {
        return [[[[self checkPersonalSubmitData] concat:[self checkInsutitutionSubmitData]] concat:[self requestSubmitNormalInfoSingal]] concat:[self requestInstitutionSummaryEditSingal]];
    }

    return [RACSignal empty];
}

@end
